Graph Q and A
What is a graph? How graph can be represented in computer?
Answer: Graph is a hierarchical data structure consisting of nodes (vertices) and edges (arcs). Graph can be used to represent computer network, cities, geographical maps of several locations, electrical circuits and many more. In computer two-dimensional array, linked list, adjacency matrix can be used to represent graph.
How graphs can be traversed?
Answer: Graph traversal means visiting each node (vertex) of graph once. Depth first traversal (DFT) or Breadth First Traversal (BFT) can be used to traverse a graph.
What is minimum spanning tree (MST)? What algorithms are used to find a minimum spanning tree of a graph?
Answer: Minimum spanning tree is a tree consisting of all the nodes of a graph with minimum weight. Dijkastras, Prims algorithms can be used to find minimum spanning tree from a graph.
What is a topological Sort?
Answer: Topological sort is a linear ordering of the vertices in a directed acyclic graph (DAG) such that for every directed edge u--> v, vertex u comes before vertex v in the ordering. Topological sort if possible for only directed acyclic graphs with no cycles.
Write the applications of topological sort.
Answer: For scheduling tasks, course prerequisites, build systems where some files must be processes before others.
For what purpose Floyd-Warshall algorithm used? What is time complexity of this algorithm? What are the applications of this algorithm?
The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices in a weighted graph (directed or undirected). Time complexity: O(V3) where V is the number of vertices. Finding shortest travel time between all pairs of cities. Network routing (e.g., computing routing tables). Game AI (pathfinding in game maps with pre-computation).
Which data structure is used to implement BFS and DFS algorithms?
Answer: Stack is used for DFS and Queue is used for BFS.
Write algorithm for Breadth First Search.
Answer:
If u is unvisited:
Mark u as visited
Enqueue u into Q
Write algorithm for depth first search.
Answer:
Write Dijkstras shortest path algorithm.
Answer:
Write Kruskals shortest path algorithm.
Answer: Kruskals MST algorithm is edge-based and its time complexity is O(E log(E)).
What are the time complexities for Dijkstras shortest path algorithm, and Kruskals algorithm for minimum spanning tree.
Answer: For Dijkstras shortest path:
If a Min-Heap (Priority Queue) is used: O(V+E) * log(V) (where V = number of vertices, E = number of edges) If an Array (No Heap) is used: O(V2) (slower, but useful for dense graphs) For Kruskals Minimum Spanning Tree: O(E*log(E)) or O(E*log(V)).
Write Prims Minimum spanning tree algorithm.
Answer: Prims MST algorithm is based on vertex-based and its time complexity is O(E*log(V)).
Write Floyd-Warshall all-short-path algorithm.
Answer: